<template>
  <view class="ZSDetail">
    <cu-form ref="ZSForm">
		<view class="flex bg-white padding solid-bottom">
			<view class="title margin-right" style="color: black;">
				专业
			</view>
			<text>{{optionsObj.MAJOR_NAME}}</text>
			
		</view>
		<view class="flex bg-white padding solid-bottom ">
			<view class="title margin-right" style="color: black;">
				专业班
			</view>
			<text>{{optionsObj.COURSENAME}}</text>
		</view>
		<view class="form-group">
		  <cu-input name="CLASSNAME" title="班级名称" required placeholder="请输入班级名称"/>
		</view>
    <view class="form-group">
        <cu-input type="number" name="MINNUM" title="最低成班人数" required placeholder="请输入最低成班人数"/>
      </view>
	  

      <view class="form-group">
        <cu-input type="number" name="MAXNUM" title="最大招生人数" required placeholder="请输入最大招生人数"/>
      </view>

      <view class="form-group">
        <cu-date-picker name="S_ZS_DATE" title="开始招生时间" required/>
      </view>

      <view class="form-group">
        <cu-date-picker name="E_ZS_DATE" title="结束招生时间"  required/>
      </view>

      <view class="form-group">
        <cu-date-picker name="YBXD_DATE" title="原班续读报名时间" required/>
      </view>

      <view class="form-group">
        <cu-date-picker name="LSZB_DATE" title="老生读报名时间"  required/>
      </view>

      <view class="form-group">
        <cu-date-picker name="S_KK_DATE" title="开课时间" required/>
      </view>

      <view class="form-group">
        <cu-date-picker name="E_KK_DATE" title="授课结束时间" required/>
      </view>

      <view class="form-group">
        <cu-input type="number" name="CLASS_HOUR" title="课次" required placeholder="请输入课次"/>
      </view>

      <view class="form-group">
        <cu-input name="PLACE" title="上课地点"  placeholder="请输入上课地点"/>
      </view>

      <view class="form-group">
        <cu-picker required name="SKSJ1" title="上课时间1" :options="SKSJOpts"/>
      </view>

      <view class="form-group">
        <cu-picker name="SKSJ2"  title="上课时间2" :options="SKSJOpts"/>
      </view>

      <view class="form-group">
        <cu-picker name="SKSJ3"  title="上课时间3" :options="SKSJOpts"/>
      </view>

      <view class="form-group">
        <cu-picker name="SKSJ4"  title="上课时间4" :options="SKSJOpts"/>
      </view>

      <view class="form-group">
        <cu-picker name="SKSJ5"  title="上课时间5" :options="SKSJOpts"/>
      </view>

      <view class="form-group">
        <cu-input type="digit" name="SFJE1" title="收费金额(50周岁以内)" required placeholder="请输入金额（单位：元）"/>
      </view>

      <view class="form-group">
        <cu-input type="digit" name="SFJE2" title="收费金额(50周岁以上)" required placeholder="请输入金额（单位：元）"/>
      </view>

      <view class="form-group">
        <cu-radio name="SH_FLAG" title="是否需要审核" :options="SH_FLAGOpts"></cu-radio>
      </view>

      <view class="form-group">
        <cu-radio name="DSZ_FLAG" title="是否单双周上课" :options="DSZ_FLAGOpts"></cu-radio>
      </view>

      <view class="form-group">
        <cu-radio name="STATE" title="是否启用" :options="STATEOpts" @change="handleSTATE"></cu-radio>
      </view>
    </cu-form>

    <view class="padding">
      <button @tap="onSubmitZSForm" class="cu-btn bg-blue block lg round shadow margin-top-lg">保存提交</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import CuInput from '../../components/cu-input/index.vue'
import CuRadio from '../../components/cu-radio/index.vue'
import CuDatePicker from '../../components/cu-datePicker/index.vue'
import CuPicker from '../../components/cu-picker/index.vue'
import CuForm from '../../components/cu-form/index.vue'
import {onLoad,onReady} from "@dcloudio/uni-app";
import {nextTick,ref,onMounted,reactive} from "vue";
import {useCourseZSDetail} from "./useCourseZSDetail";
import {FetchCourseZSDetailDAT} from "../../types/responseTypes";
import {doModifyCourseZSInfo} from "../../api/classes";
import {useStore} from "vuex";
import moment from "moment";
import {useCourseZSList} from "./useCourseZSList";
const ZSForm = ref<any>(null)
const {courseZSDetail,initCourseZSDetail} = useCourseZSDetail()
const CLASS_ID = ref('0')
const {initCourseZSList,courseZSList,total,initgetRDCourseList,ZyClassList}:any = useCourseZSList()
const curPickMajorIdx = ref<any>(-1)
const curPickMajorIdxto = ref<any>(-1)
const SelectClassName = ref<any>("")

interface CuRadioOpt {
  value: string
  label: string
}

const SH_FLAGOpts: CuRadioOpt[] = [
  {
    value: '1',
    label: '是'
  },
  {
    value: '0',
    label: '否'
  }
]

const DSZ_FLAGOpts: CuRadioOpt[] = [
	{
	  value: '0',
	  label: '无单双周'
	},
  {
    value: '1',
    label: '单周'
  },
  {
    value: '2',
    label: '双周'
  }
]

const STATEOpts: CuRadioOpt[] = [
  {
    value: '1',
    label: '是'
  },
  {
    value: '0',
    label: '否'
  }
]

const SKSJOpts= reactive<any>([])
const optionsObj = ref<any>({})

const majorOpts = ref<any>([])
onLoad(async (options: any) => {
	const SKSList =  uni.getStorageSync("SKSJOpts")
	for(let i =0;i<SKSList.length;i++){
		SKSJOpts.push(SKSList[i])
	}
	optionsObj.value = options
  if (options.CLASS_ID){
		CLASS_ID.value = options.CLASS_ID
		await initCourseZSDetail(CLASS_ID.value)
		nextTick(()=>{
			ZSForm.value.setFields([
			  'CLASSNAME',
				'CLASS_ID',
				'MAJOR_ID',
			  'MAXNUM',
				'COURSE_ID',
			  'MINNUM',
			  'S_ZS_DATE',
			  'E_ZS_DATE',
			  'YBXD_DATE',
			  'LSZB_DATE',
			  'S_KK_DATE',
			  'E_KK_DATE',
			  'CLASS_HOUR',
			  'PLACE',
			  'SKSJ1',
			  'SKSJ2',
			  'SKSJ3',
			  'SKSJ4',
			  'SKSJ5',
			  'SFJE1',
			  'SFJE2'
			].map(name => ({
			  name,
			  value: (courseZSDetail.value[name as keyof FetchCourseZSDetailDAT] || '') + ''
			})))	
		}) 
		}
		nextTick(()=>{
			ZSForm.value.setFields([
			  {
				name: 'SH_FLAG',
				value: courseZSDetail.value.SH_FLAG || SH_FLAGOpts[0].value
			  },
			  {
				name: 'DSZ_FLAG',
				value: courseZSDetail.value.DSZ_FLAG || DSZ_FLAGOpts[0].value
			  },
			  {
				name: 'STATE',
				value: courseZSDetail.value.STATE || STATEOpts[0].value
			  }
			])
		})
		
})
function handleSTATE(e){
	if(e == "0"){
		uni.showModal({
			title: '提示',
			content: '停用本班会把报名本班的缴费全部退款',
			showCancel:false,
			confirmText:"我已知晓",
			success: res => {},
			fail: () => {},
			complete: () => {}
		});
	}
}
const {state} = useStore()
function onSubmitZSForm(){
  (ZSForm.value.submit() as Promise<any>).then(async values => {
    const {CLASSNAME,MAXNUM, MINNUM, S_ZS_DATE, E_ZS_DATE, YBXD_DATE, LSZB_DATE,
      S_KK_DATE, E_KK_DATE, CLASS_HOUR, PLACE, SKSJ1, SKSJ2, SKSJ3, SKSJ4, SKSJ5, SFJE1, SFJE2,
      SH_FLAG,
      DSZ_FLAG,
      STATE
    } = values
	const datatime = moment().format('YYYY-MM-DD')
    try {
      const res = await doModifyCourseZSInfo({
        CLASS_ID: CLASS_ID.value,
        MAJOR_ID: optionsObj.value.MAJOR_ID,
        COURSE_ID: optionsObj.value.COURSE_ID,
        CLASSNAME,
        MAXNUM,
        MINNUM,
        S_KK_DATE:S_KK_DATE?S_KK_DATE:datatime,
        S_ZS_DATE:S_ZS_DATE?S_ZS_DATE:datatime,
        E_ZS_DATE:E_ZS_DATE?E_ZS_DATE:datatime,
        YBXD_DATE:YBXD_DATE?YBXD_DATE:datatime,
        E_KK_DATE:E_KK_DATE?E_KK_DATE:datatime,
        CLASS_HOUR,
        PLACE:PLACE?PLACE:"",
        SKSJ1,
        SKSJ2:SKSJ2?SKSJ2:"",
        SKSJ3:SKSJ3?SKSJ3:"",
        SKSJ4:SKSJ4?SKSJ4:"",
        SKSJ5:SKSJ5?SKSJ5:"",
        SH_FLAG,
        DSZ_FLAG,
        SFJE1,
        SFJE2,
        STATE,
        LSZB_DATE:LSZB_DATE?LSZB_DATE:datatime,
        OPERATOR_ID: state.user.userInfo.CUSER_ID
      })

      if (res.ERROR[0].errcode == '0'){
        uni.$emit('ZSUpdate')
        uni.showToast({
          title: '提交成功',
          icon: 'success',
          mask: true
        })

        setTimeout(() => {
          uni.navigateBack()
        },500)
      }else {
        uni.showToast({
          title: res.ERROR[0].error,
          icon: 'none',
          mask: true
        })
      }
    }catch (err){
      uni.showToast({
        title: '提交失败',
        icon: 'error',
        mask: true
      })
    }
  }).catch(err => {
    uni.showToast({
      title: '请完善相关资料后再提交',
      icon: 'none',
      mask: true
    })
  })
}
</script>

<style scoped lang="scss">
.ZSDetail{
  box-sizing: border-box;
  padding-bottom: constant(safe-area-inset-bottom);  ///兼容 IOS<11.2/
  padding-bottom: env(safe-area-inset-bottom);  ///兼容 IOS>11.2/
  .form-group+.form-group{
    border-top: 1upx solid #eee;
  }
}
</style>
